define([
    'jquery',
    'underscore',
    'backbone',
    'module/user/models/UserModel',
    'module/user/models/UserCollection',
    'module/helper/List',
    'text!module/user/templates/main.html',
    'text!module/user/templates/add.html',
    'text!module/user/templates/edit.html',
    'text!module/user/templates/drop.html',
    'text!module/user/templates/user_info.html',
    'text!module/user/templates/list.html',
], function($, _, Backbone, UserModel, UserCollection, List, main, add, edit, drop, user_info, list) {

    var UserController = Backbone.View.extend({
        el: $("#page"),
        initialize: function() {
            this.render();
            this.list_view(1);
        },
        events: {
            "click #list_view": "list_view",
            "click #add_view": "add_view",
            "click #edit_view": "edit_view",
            "click #drop_view": "drop_view",
            "click #add_action": "add_action",
            "click #edit_action": "edit_action",
            "click #drop_action": "drop_action",
            "click #search": "search",
            "click #pag": "pag_action"


        },
        render: function() {
            this.$el.html(main);
        }, list_view: function(pag) {
            $.get('server/api/user/find_all/' + pag, function(data) {
                var collection = new UserCollection(data.result);
                var listv = new List({collection: collection, count: data.count, pag: pag, list: list});
                listv.render();
            }, "json");
        },
        add_view: function() {
            $("#main-list").html(add);
        },
        edit_view: function(event) {
            $("#main-list").html(edit);
            var id = event.target.getAttribute('index');
            var user = new UserModel({id: id});
            user.fetch({
                success: function(data) {
                    data = data.toJSON();
                    $("#id").val(data.id);
                    $("#name").val(data.name);
                    $("#login").val(data.login);
                    $("#password").val(data.password);
                }
            });
        },
        drop_view: function(event) {
            $("#main-list").html(drop);
            var id = event.target.getAttribute('index');
            var user = new UserModel({id: id});
            user.fetch({
                success: function(data) {
                    data = data.toJSON();
                    $("#id").val(data.id);
                    $("#name").val(data.name);
                    $("#login").val(data.login);
                    $("#password").val(data.password);
                }
            });
        },
        add_action: function() {
            var self = this;
            var user = new UserModel();
            var userDetails = {
                name: $("#name").val(),
                login: $("#login").val(),
                password: $("#password").val(),
                role: $("#role option:selected").val()
            };
            user.save(userDetails, {
                success: function(data) {
                    self.list_view(1);
                }
            });
        },
        edit_action: function() {
            var self = this;
            var user = new UserModel();
            var userDetails = {
                id: $("#id").val(),
                name: $("#name").val(),
                login: $("#login").val(),
                password: $("#password").val(),
                role: $("#role option:selected").val()
            };
            user.save(userDetails, {
                success: function(data) {
                    self.list_view(1);
                }
            });
        },
        drop_action: function() {
            var self = this;
            var user = new UserModel({
                id: $("#id").val()
            });
            user.destroy({
                success: function(data) {
                    self.list_view(1);
                }
            });
        },
        search: function() {
            var search = $('#txt_search').val();
            $.ajax({
                url: 'server/api/user/search/' + search,
                type: 'GET',
                dataType: "json",
                success: function(data) {
                    var collection = new UserCollection(data.result);
                    var list = new List({collection: collection, count: data.count});
                    list.render();
                }
            });
        },
        pag_action: function(event) {
            var pag = event.target.getAttribute('index');
            this.list_view(pag);
        },
        user_info: function(event) {
            this.$el.html(user_info);

            var user = new UserModel({id: $("#user_id").val()});
            user.fetch({
                success: function(data) {
                    data = data.toJSON();
                    $("#id").val(data.id);
                    $("#name").val(data.name);
                    $("#login").val(data.login);
                    $("#password").val(data.password);
                }
            });
        }

    });
    return UserController;
});
